/**
 * 渲染订单列表
 */
Order = function () {
};

/**
 * 设置数据集
 */
Order.prototype.setData = function (data) {
	Order.prototype.data = data;
};

/**
 * 列名数据
 */
Order.prototype.cols = [
	{
		title: '<span>商品</span>',
		width: "20%",
		className: "product-info",
		template: function (orderitem, order) {
			var h = '<div class="img-block">';
			h += '<img layer-src src="' + ns.img(orderitem.sku_image,'small') + '">';
			h += '</div>';
			h += '<div class="info">';
			h += '<a href="' + ns.url("shop/order/detail", {order_id: orderitem.order_id}) + '" target="_blank" title="' + orderitem.sku_name + '" class="ns-multi-line-hiding ns-text-color">' + orderitem.sku_name + '</a>';
			h += '<p>' + orderitem.goods_class_name + '</p>';
			if (orderitem.refund_status_name != '') {
				h += '<br/><a href="' + ns.url("shop/orderrefund/detail", {order_goods_id: orderitem.order_goods_id}) + '"  target="_blank" class="ns-text-color">' + orderitem.refund_status_name + '</a>&nbsp;&nbsp;';
			}
			return h;
		}
	},
	{
		title: "单价/数量/账期",
		width: "10%",
		align: "left",
		className: "order-price",
		template: function (orderitem, order) {
			var billing_name = '';
			if (order.billing_type == 1) {
				billing_name = '月结+' + order.billing_ratio + '%';
			} else if (order.billing_type == 2) {
				billing_name = '季结+' + order.billing_ratio + '%';
			} else if (order.billing_type == 3) {
				billing_name = '半年结+' + order.billing_ratio + '%';
			}

			var h = '<div style="padding-right: 15px;">';
			h += '<div>';
			h += '<span>' + (orderitem.real_goods_money / orderitem.num).toFixed(2) + '</span>';
			h += '</div>';
			h += '<div>';
			h += '<span>' + orderitem.num + '件</span>';
			h += '</div>';
			h += '<div>';
			h += '<span style="color: green;font-weight: bold;">' + billing_name + '</span>';
			h += '</div>';
			h += '</div>';
			return h;
		}
	},
	{
		title: "实付金额",
		width: "10%",
		align: "center",
		className: "order-money",
		merge: true,
		template: function (orderitem, order) {
			// var billing_name = '';
			// if (order.billing_type == 1) {
			// 	billing_name = '月结';
			// } else if (order.billing_type == 2) {
			// 	billing_name = '季结';
			// } else if (order.billing_type == 3) {
			// 	billing_name = '半年结';
			// }
			var h = '<div style="padding-right: 15px;">';
			// if (order.billing_money > 0) {
			// 	billing_money += '<span style="color: green;font-weight: bold;">（'+ billing_name +'+' + order.billing_money + '）</span>';
			// }
			// h += '<span>' + order.order_money + billing_money + '</span>';
			h += '<span>' + order.order_money + '</span>';
			h += '</div>';
			return h;
		}
	},
	{
		title: "收货人信息",
		width: "15%",
		align: "left",
		className: "buyers",
		merge: true,
		template: function (orderitem, order) {
			var h = '';
			if (order.order_type != 4) {
				h += '<p>';
				h += '<a href="javascript:;">' + order.name + '</a>';
				h += '</p>';
				h += '<span>' + order.mobile + '</span>';
				h += '<span class="ns-line-hiding" title="' + order.full_address + '">' + order.full_address + '</span>';
			} else {
				h = '<p>';
				h += '<span>' + order.mobile + '</span>';
				h += '</p>';
			}

			return h;
		}
	},
	{
		title: "审批状态",
		width: "10%",
		align: "center",
		className: "check-status",
		merge: true,
		template: function (orderitem, order) {
			if (order.check === 1){
				return '<img layer-src src="' + ns.img(order.check_img, '') + '" height="100"/>';
			}else{
				return '<div style="color: red;font-weight: bold;">' + order.check_name + '</div>';
			}
		}
	},
	{
		title: "供应商信息",
		width: "10%",
		align: "left",
		className: "offer-status",
		// merge: true,
		template: function (orderitem, order) {
			return '<div>' + orderitem.offer_name + '</div>';
		}
	},
	{
		title: "交易状态",
		width: "10%",
		align: "center",
		className: "transaction-status",
		// merge: true,
		template: function (orderitem, order) {
			var html = '<div>' + order.order_status_name + '</div>';
			if (order.order_status_name == '待发货' && orderitem.offer == 0){
				html = '<div>' + '待下单' + '</div>';
			}
			if (order.order_status_name == '待发货' && orderitem.offer == 1 && orderitem.review == 0){
				html = '<div style="color: red">' + '预下单待审核' + '</div>';
			}
			if (order.delivery_status == 1){
				html = '<div>' + '已发货' + '</div>';
			}
			if (order.delivery_status == 2){
				html = '<div>' + '已收货' + '</div>';
			}
			if (order.take_delivery_img){
				try{
					html = '';
					order.take_delivery_img = order.take_delivery_img.split(',');
					$.each(order.take_delivery_img, function (i, v) {
						html += '<img layer-src src="' + ns.img(v) + '" height="100"/>';
					});
				}catch (e) {}
			}
			html += '<div>' + order.promotion_type_name;
			html += order.promotion_status_name != '' ? '(' + order.promotion_status_name + ')' : '';
			html += '</div>';
			return html;
		}
	},
	{
		title: "操作",
		width: "10%",
		align: "center",
		className: "operation",
		// merge: true,
		template: function (orderitem, order) {
			var url = "shop/order/detail";
			var html = '';
			var action_json = order.order_status_action;
			var action_arr = JSON.parse(action_json);
			var action = action_arr.action;
			html += '<div class="ns-table-btn">';
			console.log(orderitem);
			for (var k = 0; k < action.length; k++) {
					//未审批隐藏下单
				if ((parseInt(order.check) === 0 && action[k].action === 'orderOffer')) {
					//未审批、已推送隐藏推送功能
				}else if ((parseInt(order.check) === 0 && parseInt(order.tip_leader) === 1 && action[k].action === 'pushToApprover')) {
					//非供应商隐藏发货
				}else if(parseInt(group_id) !== 5 && action[k].action === 'orderDelivery'){
					//供应商隐藏确认收货
				}else if(parseInt(group_id) === 5 && action[k].action === 'takeDelivery'){
					//已下单隐藏下单
				}else if(parseInt(orderitem.offer) === 1 && action[k].action === 'orderOffer') {
					//审批人已审核隐藏改价
				}else if(parseInt(order.check) === 1 && action[k].action === 'orderAdjustMoney') {
					//是管理员组，已下单, 未审核 显示审核及其他按钮
				}else if(parseInt(group_id) == 1 && parseInt(orderitem.offer) === 1 && parseInt(orderitem.review) === 0){
					html += '<a class="layui-btn" href="javascript:orderAction(\'review\', ' + orderitem.order_goods_id + ')">管理员审核</a>';
					html += '<a class="layui-btn" href="javascript:orderAction(\'' + action[k].action + '\', ' + order.order_id + ')">' + action[k].title + '</a>';
					//已下单什么也不显示
				}else if (action[k].action === 'orderOffer'){
					//已收货什么也不显示
				}else if(orderitem.delivery_status === 1 || orderitem.delivery_status === 2) {
					//供应商不准修改客户收货地址
				}else if (parseInt(group_id) === 5 && action[k].action === 'orderAddressUpdate') {
					//供应商应该发货时，且没有呼叫货拉拉，还需要显示呼叫货拉拉按钮
				}else if (parseInt(group_id) === 5 && orderitem.hll.length == 0 && action[k].action === 'orderDelivery') {
					html += '<a class="layui-btn" href="javascript:orderAction(\'tel_hll\', ' + orderitem.order_goods_id + ')">呼叫货拉拉</a>';
					html += '<a class="layui-btn" href="javascript:orderAction(\'' + action[k].action + '\', ' + order.order_id + ')">' + action[k].title + '</a>';
					//供应商应该发货时，已呼叫过货拉拉，需要显示货拉拉订单号，显示取消订单按钮
				}else if (parseInt(group_id) === 5 && orderitem.hll.length > 0 && action[k].action === 'orderDelivery'){
					html += '<a class="layui-btn" href="javascript:;" style="color: #000;cursor: unset;display: block;">货拉拉订单号：' + orderitem.hll + '</a>';
					// html += '<a class="layui-btn" href="javascript:;" style="color: #000;cursor: unset;">司机姓名：' + orderitem.hll_name + '，电话：'+orderitem.hll_phone_no+'，车牌号：'+orderitem.hll_license_plate+'</a>';
					//司机信息，定时任务查
					if (orderitem.hll_name){
						html += '<a class="layui-btn" href="javascript:;" style="color: #000;cursor: unset;display: block;">司机姓名：' + orderitem.hll_name + '</a>';
						html += '<a class="layui-btn" href="javascript:;" style="color: #000;cursor: unset;display: block;">司机电话：'+orderitem.hll_phone_no+'</a>';
						html += '<a class="layui-btn" href="javascript:;" style="color: #000;cursor: unset;display: block;">司机车牌号：'+orderitem.hll_license_plate+'</a>';
					}
					html += '<a class="layui-btn" href="javascript:orderAction(\'hll_order_cancel\', ' + orderitem.order_goods_id + ')">取消货拉拉订单</a>';
					html += '<a class="layui-btn" href="javascript:orderAction(\'' + action[k].action + '\', ' + order.order_id + ')">' + action[k].title + '</a>';
					//显示剩下的按钮
				}else{
					html += '<a class="layui-btn" href="javascript:orderAction(\'' + action[k].action + '\', ' + order.order_id + ')">' + action[k].title + '</a>';
				}
			}
			html += '</div>';
			return html;

		}
	},
	{
		title: "下单",
		width: "10%",
		align: "center",
		className: "operation",
		// merge: true,
		template: function (orderitem, order) {
			var url = "shop/order/detail";
			var html = '';
			var action_json = order.order_status_action;
			var action_arr = JSON.parse(action_json);
			var action = action_arr.action;
			html += '<div class="ns-table-btn">';
			for (var k = 0; k < action.length; k++) {
				if (action[k].action === 'orderOffer' && orderitem.offer === 0 && orderitem.review === 0){
					html += '<a class="layui-btn" href="javascript:orderActionOffer(\'' + action[k].action + '\', ' + orderitem.order_goods_id + ')">' + action[k].title + '</a>';
				}
			}
			html += '</div>';
			return html;

		}
	}
];

/**
 * 渲染表头
 */
Order.prototype.header = function (hasThead) {
	var colgroup = '<colgroup>';
	var thead = '';
	if (hasThead) thead = '<thead><tr>';

	for (var i = 0; i < this.cols.length; i++) {
		var align = this.cols[i].align ? "text-align:" + this.cols[i].align : "";

		colgroup += '<col width="' + this.cols[i].width + '">';
		if (hasThead) {
			thead += '<th style="' + align + '" class="' + (this.cols[i].className || "") + '">';
			thead += '<div class="layui-table-cell">' + this.cols[i].title + '</div>';
			thead += '</th>';
		}
	}
	colgroup += '</colgroup>';
	if (hasThead) thead += '</tr></thead>';
	return colgroup + thead;
};

/**
 * 渲染内容
 */
Order.prototype.tbody = function () {

	var tbody = '<tbody>';
	for (var i = 0; i < this.data.list.length; i++) {

		var order = this.data.list[i];
		var orderitemList = order.order_goods;
		var pay_type_name = order.pay_type_name != '' ? order.pay_type_name : "";

		if (i > 0) {
			//分割行
			tbody += '<tr class="separation-row">';
			tbody += '<td colspan="' + this.cols.length + '"></td>';
			tbody += '</tr>';
		}

		//订单项头部
		tbody += '<tr class="header-row">';
		tbody += '<td colspan="8">';
		tbody += '<span class="order-item-header" style="margin-right:10px;">订单号：' + order.order_no + '</span>';
		tbody += '<span class="order-item-header ns-text-color more" style="margin-right:50px;" onclick="showMore(' + order.order_id + ')">更多';
		tbody += '<div class="more-operation" data-order-id="' + order.order_id + '">';
		tbody += '<span>支付流水号：' + order.out_trade_no + '</span>';
		tbody += '</div></span>';

		tbody += '<span class="order-item-header" style="margin-right:50px;">下单时间：' + ns.time_to_date(order.create_time) + '</span>';
		tbody += '<span class="order-item-header" style="margin-right:50px;">订单类型：' + order.order_type_name + '</span>';
		if (pay_type_name) tbody += '<span class="order-item-header">支付方式：' + pay_type_name + '</span>';

		tbody += '</td>';
		tbody += '<td>';
		tbody += '<div class="ns-table-btn">';
		if (order.order_type == 1) {
			// if (group_id == 5) {
			//
			// } else {
			// 	tbody += '<a class="layui-btn layui-btn-xs" href="' + ns.url('shop/order/printOrder', {order_id: order.order_id}) + '" target="_blank" >打印发货单</a>';
			// }
			tbody += '<a href="'+ ns.url('shop/order/printOrder2',{order_id:order.order_id}) +'" target="_blank" class="layui-btn layui-btn-xs">客户发货单</a>';
			tbody += '<a class="layui-btn layui-btn-xs" href="' + ns.url('shop/order/printOrder', {order_id: order.order_id}) + '" target="_blank" >原始发货单</a>';
		}

		if (order.order_status == -1) {
			tbody += '<a class="layui-btn layui-btn-xs" href="javascript:orderDelete(' + order.order_id + ');" >删除</a>';
		}
		tbody += '<a class="layui-btn layui-btn-xs" href="' + ns.url("shop/order/detail", {order_id: order.order_id}) + '" target="_blank">查看详情</a>';
		tbody += '<a class="layui-btn layui-btn-xs" href="javascript:orderRemark(' + order.order_id + ');">备注</a> ';
		if (order.order_status == 0) {
			tbody += '<a class="layui-btn layui-btn-xs" href="javascript:offlinePay(' + order.order_id + ');">线下支付</a> ';
		}

		tbody += '</div>';
		tbody += '</td>';
		tbody += '</tr>';

		// tbody += '<tr class="separation-row"><td colspan="6"><hr /></td></tr>';

		var orderitemHtml = '';
		loadImgMagnify();
		for (var j = 0; j < orderitemList.length; j++) {
			var orderitem = orderitemList[j];
			orderitemHtml += '<tr class="content-row">';
			for (var k = 0; k < this.cols.length; k++) {

				if (j == 0 && this.cols[k].merge && this.cols[k].template) {

					orderitemHtml += '<td class="' + (this.cols[k].className || "") + '" align="' + (this.cols[k].align || "") + '" style="' + (this.cols[k].style || "") + '" rowspan="' + orderitemList.length + '">';
					orderitemHtml += this.cols[k].template(orderitem, order);
					orderitemHtml += '</td>';

				} else if (this.cols[k].template && !this.cols[k].merge) {

					orderitemHtml += '<td class="' + (this.cols[k].className || "") + '" align="' + (this.cols[k].align || "") + '" style="' + (this.cols[k].style || "") + '">';
					orderitemHtml += this.cols[k].template(orderitem, order);
					orderitemHtml += '</td>';

				}
			}
			orderitemHtml += '</tr>';
		}

		tbody += orderitemHtml;

		if (order.remark != '') {
			tbody += '<tr class="remark-row">';
			tbody += '<td colspan="' + this.cols.length + '">备注：' + order.remark + '</td>';
			tbody += '</tr>';
		}

	}

	tbody += '</tbody>';
	return tbody;
};

/**
 * 渲染表格
 */
Order.prototype.fetch = function () {
	if (this.data.list.length > 0) {
		return '<table class="layui-table layui-form">' + this.header(true) + '</table><table class="layui-table order-list-table layui-form">' + this.header(false) + this.tbody() + '</table>';
	} else {
		return '<table class="layui-table order-list-table layui-form">' + this.header(true) + '</table>' + '<div class="order-no-data-block"><ul><li><i class="layui-icon layui-icon-tabs"></i> </li><li>暂无订单</li></ul></div>';
	}
};

function showMore(order_id) {
	$(".more-operation[data-order-id]").hide();
	$(".more-operation[data-order-id='" + order_id + "']").show();
	$("body").click(function (e) {
		if (!$(e.target).closest(".order-item-header.more").length) {
			$(".more-operation[data-order-id='" + order_id + "']").hide();
		}
	});
}